<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of login
 *
 * @author cs321ga3a
 */
class login extends CI_Controller {
    function __construct()
    {
        parent::__construct();
    }

    function index() {
        if ($this->session->userdata('user_id')){
            redirect(base_url()."meals");
        }
		
        $data = array(      
            'title' => 'RMPLANNER',
            'payload' => $this->session->flashdata('message'),
            'view' => 'view_login',
			'settings' => false,
			'username' => $this->input->cookie('username')
			);
			
        echo $this->session->flashdata('message');
        $this->load->view('view_template', $data);
    }
	
	
    function doLogin(){
		$username = "";
		$password = "";
		$username = $this->input->post('username');
		$password = $this->input->post('password');
				
		if ($username == null || $password == null){
			$this->session->set_flashdata('message', "Invalid post data");
			redirect();
		}
		
		$enc_pass = encrypt_password($password);
				
		$cookie = array(
			'name'   => 'username',
			'value'  => $username,
			'expire' => time() + (20 * 365 * 24 * 60 * 60),
			'domain' => '.rmplanner.net',
			'path'   => '/',
			'prefix' => 'rmplanner_',
			'secure' => TRUE
		);
		
		
		//$this->input->set_cookie('username', $cookie);

        $url = kitchen_base_url()."login";
        $args = array(
                	'user' => $username,
					'pass' => $enc_pass
                );
                
	  $result = $this->curl->simple_post($url, $args);

	  if ($result == 0){
		echo "Invalid username/password combination";
	  }
	  else{
            $this->session->set_userdata(array(
                'user_id' => $result,
				'username' => $username
            ));
			echo "good";
	  }
        return;
    }
	
	function doLogout(){
		$this->session->unset_userdata('user_id');
		
		$cookie = array(
			'name'   => 'username',
			'value'  => "",
			'expire' => 0,
		);

		$this->input->set_cookie($cookie);
		
		redirect(base_url()+'/login/');
	}

        
        
        
    
}
?>
